/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.rh.game.loader;

import com.rh.game.Character;
import com.rh.game.Character.BodyPart;
import com.rh.game.GameEffect;
import com.rh.game.MagicalEffect;

import com.rh.game.impl.GameClothing1;
import com.rh.game.impl.GameCondition1;
import com.rh.game.impl.GameEffect1;
import com.rh.game.impl.GameMoney1;
import com.rh.game.impl.GameObject1;
import com.rh.game.impl.MagicalEffect1;

import com.rh.util.Parser;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Hayden
 */
public class GameObjectBuffer {
    private static void debug(Object o)  {
        // System.out.println("GameObjectBuffer: " + o);
    }
    
    public static enum Mode  {
        LOCATION;
    }
    
    private Mode mode = null;
    
    private List<GameObject1> objects = new ArrayList<GameObject1>();
    List<GameObject1> getObjects()  { return objects; }
    
    private String startElement = null;
    
    private String id = null;
    private String name = null;
    private String money = "";
    
    private LocationBuffer locBuffer = new LocationBuffer();
    
    private String bodyPart = "";
    private String when = null;
    private String type = "";
    private String whom = null;    

    private void init()  {
        id = null;
        name = null;
        money = "";
    
        locBuffer = new LocationBuffer();
    
        bodyPart = "";
        when = null;
        type = "";
        whom = null;            
    }
    
    void characters(String pValue)  {
        debug(startElement + " ==> " + pValue);
        
        pValue = Parser.trim(pValue);
        if (pValue.length() == 0)  return;

        if (mode == Mode.LOCATION)  {
            locBuffer.characters(pValue);
        }
        else if ("ID".equals(startElement))  {
            id = pValue;
        }
        else if ("Name".equals(startElement))  {
            name = pValue;
        }
        else if ("Money".equals(startElement))  {
            money = pValue;
        }
        else if ("BodyPart".equals(startElement))  {
            bodyPart = pValue;
        }
        else if ("When".equals(startElement))  {
            when = pValue;
        }
        else if ("Type".equals(startElement))  {
            type = pValue;
        }
        else if ("Whom".equals(startElement))  {
            whom = pValue;
        }        
    }
    
    void startElement(String pName)  {
        startElement = pName;
        if ("location".equals((pName)))  {
            mode = Mode.LOCATION;
        }
    }
    
    void endElement(String pName)  {
        if ("object".equals(pName))  {
            debug(
                    "Spit out: " + id + ", " + name+ ", " + money + ", " + 
                    locBuffer.getLocation() + ", " + bodyPart + ", " + 
                    when + ", " + type + ", " + whom);
            
            if (money.length() > 0)  {
                objects.add(new GameMoney1(id, name, locBuffer.getLocation()));
            }
            else if (bodyPart.length() > 0)  {
                BodyPart bp = BodyPart.valueOf(bodyPart);
                List<MagicalEffect> effects = new ArrayList<MagicalEffect>();
                if (type.length() > 0)  {
                    GameEffect ge = GameEffect1.m2f();
                    GameCondition1 gc = GameCondition1.isMale(Character.WEARER);
                    MagicalEffect1 me1 = new MagicalEffect1(ge, gc);
                    effects.add(me1);
                }
                GameClothing1 gc1 = new GameClothing1(id, name, locBuffer.getLocation(), bp, effects);
                objects.add(gc1);
            }
            else  {
                
            }
            
            init();
        }
        else if ("location".equals((pName)))  {
            mode = null;
        }
        
    }    
}
